<?php
namespace Common\Api;

class WeiboConnect {

    var $conifg = array();

    // 初始化一些变量
    function WeiboConnect() {
        $this->conifg['appid'] = '2825390497'; //填写APP ID
        $this->conifg['appkey'] = '70a631f8ecb21ead535871d655942c46'; //填写你APP KEY
        $this->conifg['scope'] = 'get_user_info'; //API
        $this->conifg['callback'] = 'www.jikequ.com/index.php?s=/login/weibobind'; //回调地址
    }

    // 登录地址
    function weibo_loginurl() {
        $callback = $this->conifg['callback'];
        $_SESSION['state'] = md5(uniqid(rand(), true));
		
        $login_url = "https://api.weibo.com/oauth2/authorize?client_id=". $this->conifg['appid'] 
					. "&response_type=code&redirect_uri=". urlencode($callback);
        return $login_url;
    }

    // 获取access_token
    function weibo_callback($code) {
        $callback = $this->conifg['callback'];
		
        $token_url = "https://api.weibo.com/oauth2/access_token?client_id=" . $this->conifg['appid'] 
		. "&client_secret=". $this->conifg['appkey'] 
		. "&grant_type=authorization_code&redirect_uri=" . urlencode($callback) . "&code=CODE";

        $response = file_get_contents($token_url);
        if (strpos($response, "callback") !== false) {
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
            $msg = json_decode($response);
            if (isset($msg->error)) {
                $error = "<h3>error:</h3>" . $msg->error;
                $error .= "<h3>msg  :</h3>" . $msg->error_description;
                die($error);
            }
        }

        $params = array();
        parse_str($response, $params);

        $_SESSION["access_token"] = $params["access_token"];
    }

    // 获取 Open id
    function get_weibo_uid() {
        $getinfo_url = "https://api.weibo.com/oauth2/get_token_info?access_token=" . $_SESSION['access_token'];

        $str = file_get_contents($getinfo_url);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
        }

        $user = json_decode($str);
        if (isset($user->error)) {
            $error = '<h6>错误代码:' . $user->error . '</h6>';
            $error .= '<p>提示:' . $user->error_description . '</p>';
            die($error);
        }
        $_SESSION["weibo_uid"] = $user->uid;
    }

    
    function get_user_info($access_token, $weibo_uid) {
        $user_info_url = 'https://api.weibo.com/2/users/show.json?';

        $url = $user_info_url
                . 'source=' . $this->conifg['appid']
                . '&access_token=' . $access_token
                . '&uid=' . $weibo_uid ;

        $str = file_get_contents($url);

        $user_info = json_decode($str);

        if ($user_info->ret !== 0) {
            $error = '<h6>错误代码:' . $user_info->ret . '</h6>';
            $error .= '<p>提示:' . $user_info->msg . '</p>';
        }

        return $user_info;
    }

}
?>